package tt.dz.appaction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import tt.dz.appaction.base.BaseController;
import tt.dz.common.Constants;
import tt.dz.common.PublicFunc;
import tt.dz.entity.UserFundCurr;
import tt.dz.entity.UserWallet;
import tt.dz.service.UserFundCurrService;
import tt.dz.service.UserWalletService;



/**
 * 资金流水控制层
 * @author Xielf
 *
 */
@Controller
@RequestMapping("/userFundCurr")
public class UserFundCurrController extends BaseController {

	/**资金流水业务接口*/
	@Resource
	UserFundCurrService userFundCurrService;
	
	/**用户钱包业务接口*/
	@Resource
	UserWalletService userWalletService;
	
	/**
	 * 添加资金操作流水
	 * @param evc_number 电装号
	 * @param trade_behavior	行为
	 * @param user_pay_card	用户卡号
	 * @param trade_expense	支出
	 * @param trade_income	收入
	 * @param trade_comment	说明
	 * @param request
	 * @param response
	 */
	@RequestMapping("/addUserFundCurr.do")
	public void addUserFundCurr(String evc_number,Integer trade_behavior,
			String user_pay_card,Double trade_expense,Double trade_income,String trade_comment,
			HttpServletRequest request,HttpServletResponse response){
		String resultStr = "";
		try {
			UserFundCurr userFundCurr = new UserFundCurr();
			//行为动作
			userFundCurr.setDeal_action(trade_behavior);
			
			//消费
			if(trade_expense==null || "".equals(trade_expense)){
				userFundCurr.setDeal_expend(0.00);
			}
			else{
				userFundCurr.setDeal_expend(trade_expense);
			}
			//收入
			if(trade_income==null || "".equals(trade_income)){
				userFundCurr.setDeal_income(0.00);
			}
			else{
				userFundCurr.setDeal_income(trade_income);
			}
			
			//说明
			userFundCurr.setRemark(trade_comment);
			/**添加资金操作记录*/
			String code = userFundCurrService.addUserFundCurr(userFundCurr,user_pay_card,evc_number);
			resultStr = PublicFunc.getOutStr(code, "ok", userFundCurr.getDeal_currt_num());
		} catch (Exception e) {
			e.printStackTrace();
			resultStr = PublicFunc.getOutStr(Constants.JsonResponseCode.DATABASE_OP_ERROR, e.getMessage(), null);
		}
		this.outText(resultStr, response);
	}
	/**
	 * 通过用户ID 获取消费记录
	 * @param userId 用户ID
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getUserFundCurr.do")
	public void getUserFundCurr(Integer userId,Integer pageNo,Integer pageSize,HttpServletRequest request,HttpServletResponse response){
		
		String resultStr = "";
		try {
			//获取消费记录列表
			List<UserFundCurr> userFundCurrs = userFundCurrService.getUserFundCurrByUserId(userId,pageNo,pageSize);
			//JSON节点数据MAP
			Map<String,List<UserFundCurr>> userFundMap = new HashMap<String,List<UserFundCurr>>();
			//用户消费记录JSON节点
			userFundMap.put("userFundCurrList", userFundCurrs);
			resultStr = PublicFunc.getOutStr(Constants.JsonResponseCode.SUCCESS, "ok", userFundMap);
		} catch (Exception e) {
			e.printStackTrace();
			resultStr = PublicFunc.getOutStr(Constants.JsonResponseCode.DATABASE_OP_ERROR, e.getMessage(), null);
		}
		this.outText(resultStr, response);
		
	}
	
	@RequestMapping("bluntIs.do")
	public void bluntIs(String dealCurrNum, HttpServletRequest request,HttpServletResponse response)
	{
		String resultStr = "";
		try {
			//获取消费记录列表
			UserFundCurr userFundCurr = userFundCurrService.getUserFundCurrByDealNum(dealCurrNum);
			UserWallet userWallet = userWalletService.getUserWalletByUserId(userFundCurr.getUser_id());
			double opMoney=0.0;
			switch(userFundCurr.getDeal_action())
			{
			case 101:
				opMoney=0-userFundCurr.getDeal_income();
				break;
			case 102:
				opMoney=0-userFundCurr.getDeal_income();
				break;
			case 103:
				opMoney=0-userFundCurr.getDeal_income();
				break;
			case 104:
				opMoney=0-userFundCurr.getDeal_income();
				break;
			case 201:
				opMoney=userFundCurr.getDeal_expend();
				break;
			case 202:
				opMoney=userFundCurr.getDeal_expend();
				break;
			case 301:
				opMoney=userFundCurr.getDeal_expend();
				break;
			}
			userWallet.setBalance(userWallet.getBalance()+opMoney);
			userWalletService.updateUserWallet(userWallet);
			resultStr = PublicFunc.getOutStr(Constants.JsonResponseCode.SUCCESS, "ok", null);
		} catch (Exception e) {
			e.printStackTrace();
			resultStr = PublicFunc.getOutStr(Constants.JsonResponseCode.DATABASE_OP_ERROR, e.getMessage(), null);
		}
		this.outText(resultStr, response);
	}
	
}
